package cn.xiuxian.dao.impl;

import cn.xiuxian.dao.IVideoDao2;
import cn.xiuxian.model.dto.PageInfo;
import cn.xiuxian.model.pojo.Video;
import cn.xiuxian.util.JdbcTemplate;

import java.sql.PreparedStatement;
import java.util.List;

public class VideoDaoImpl2 implements IVideoDao2 {
    @Override
    public List<Video> findAllVideoByUserid(Integer userid) {
        return JdbcTemplate.queryList(Video.class,"select  *  from  jdbc_video  where  user_id = ?",userid);
    }

    @Override
    public List<Video> findPage(String title, Integer pageNum, Integer pageSize) {
        return JdbcTemplate.queryList(Video.class,"select * from jdbc_video where  title  like  ? limit ?,?","%"+title+"%",pageNum,pageSize);
    }

    @Override
    public List<Video> findAll() {
        return JdbcTemplate.getAll(Video.class);
    }

    @Override
    public Integer findcount(String title) {
        return (Integer) JdbcTemplate.executeQuery(connection -> {
            String  sql = "select  count(*)  from jdbc_video  where  title  like ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,"%"+title+"%");
            return  preparedStatement;
        },resultSet -> {
            Integer  count = 0;
            if(resultSet.next()){
                count = resultSet.getInt(1);
            }

            return  count;
        });
    }
}
